package org.databind.controller;


import java.util.HashMap;
import java.util.Map;

import org.databind.entity.Admin;
import org.databind.entity.User;
import org.databind.entity.UserList;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/databind")
public class DataBinding {

	//http://localhost:8080/databind/param?age=100
	@RequestMapping(value="/param",method=RequestMethod.GET)
	@ResponseBody
	public String param(Integer age){
		return "age:"+age;
	}
	
	//http://localhost:8080/databind/array?name=lilei&name=hanhan&name=haha
	@RequestMapping(value="/array",method=RequestMethod.GET)
	@ResponseBody
	public StringBuffer array(String[] name){
		StringBuffer names = new StringBuffer();
		for(String temp:name){
			names.append(temp);
		}
		return names;
	}
	
	//http://localhost:8080/databind/user?name=lilei&age=100
	//http://localhost:8080/databind/user?name=lilei&age=100&connectInfo.phone=13754631232
	@RequestMapping(value="/user",method=RequestMethod.GET)
	@ResponseBody
	public User user(User user){
		return user;
	}
	
	//http://localhost:8080/databind/admin?user.name=lilei&age=100&connectInfo.phone=13754631232&admin.name=haha
	@RequestMapping(value="/admin",method=RequestMethod.GET)
	@ResponseBody
	public String admin(User user,Admin admin){
		return user.toString()+"------"+admin.toString();
	}
	
	//http://localhost:8080/databind/list?users[0].name=lilei&users[0].age=100&users[0].connectInfo.phone=13754631232&users[1].name=haha
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public String list(UserList list){
		return list.toString();
	}
	
	//{name:'yang',age:27,connectInfo:{phone:'123333323',address:'南京凝霜路'}}
	@RequestMapping(value="/json",method=RequestMethod.POST)
	@ResponseBody
	public String json(@RequestBody User user){
		return user.toString();
	}
	
	/*
	{
		name:'yang',
		age:20,
		userList:[
		{name:'zhangsan',age:38},
		{name:'lisi',age:36,connectInfo:{phone:'123333323',address:'南京凝霜路'}},
		{name:'wangwu',age:28}
		]
	}
	*/
	@RequestMapping(value="/map",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> map(@RequestBody Map<String, Object> map){
		System.out.println(map.get("name"));
		System.out.println(map.get("age"));
		System.out.println(map.get("userList"));
		return map;
	}
	
	
	@RequestMapping(value="/test",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> test(@RequestBody Map<String, Object> map){
		Map<String, String> map1 = new HashMap<String,String>();
		map1.put("name", "大方和开发商sdada");
		map1.put("age", "34");
		return map1;
	}
	
	@InitBinder("user")
	public void initUser(WebDataBinder binder){
		binder.setFieldDefaultPrefix("user.");
	}
	
	@InitBinder("admin")
	public void initAdmin(WebDataBinder binder){
		binder.setFieldDefaultPrefix("admin.");
	}
	
}
